define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop_order/index' + location.search,
                    add_url: 'shop_order/add',
                    edit_url: 'shop_order/edit',
                    del_url: 'shop_order/del',
                    multi_url: 'shop_order/multi',
                    import_url: 'shop_order/import',
                    table: 'shop_order',
                }
            });

            var table = $("#table");

            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                // console.log(data);
                //这里我们手动设置底部的值
                $("#total").text(data.extend.total);
                $("#payment").text(data.extend.payment);
                // $("#adddon").text(data.extend.adddon);
                $("#chargeback").text(data.extend.chargeback);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible: true,// 默认显示搜索条件
                search: false,// 搜索框
                // commonSearch: false,// 搜索按钮
                showToggle: false,// 切换视图
                showColumns: false,// 显示列
                showExport: false,// 导出按钮
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),  operate: false},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        // {field: 'trade_no', title: __('Trade_no'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('下单会员'), operate: 'LIKE'},
                        // {field: 'addres_id', title: __('Addres_id')},
                        // {field: 'used_score', title: __('Used_score')},
                        // {field: 'score_money', title: __('Score_money'), operate:'BETWEEN'},
                        // {field: 'user_coupon_id', title: __('User_coupon_id')},
                        // {field: 'coupon_money', title: __('Coupon_money'), operate:'BETWEEN'},
                        // {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'payamount', title: __('Payamount'), operate:'BETWEEN'},
                        // {field: 'paytype', title: __('Paytype'), operate: 'LIKE'},
                        {field: 'paid', title: __('支付状态'), formatter: Table.api.formatter.label, searchList: {0: __('否'), 1: __('是')}},
                        // {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'memo', title: __('Memo'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'area', title: __('省、市、区'), visible:false, searchList: function (column) {return Template('sourcetpl', {});}},
                        {field: 'region', title: __('省、市、区'),  operate: false},
                        {field: 'address.name', title: __('收货人'),  operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'address.mobile', title: __('联系电话'),  operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'address.address', title: __('收货地址'),  operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'status', title: __('状态'), formatter: Table.api.formatter.status, searchList: {0: __('待发货'), 1: __('待收货'), 2: __('已完成'), 3: __('已评价'), 100: __('已取消'), 101: __('已退款')}},

                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            align:'right',
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: '确认退款',
                                    text: __('确认退款'),
                                    title: __('确认退款'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-close',
                                    url: 'shop_order/cancel',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status == 100 && row.refund_status == 0){
                                            return true;
                                        }
                                        return false;
                                    },
                                    success: function (data, ret) {
                                        // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        Toastr.success('操作成功');
                                        $("#table").bootstrapTable('refresh',{});
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        cancel: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
